function gadget:GetInfo()
	return {
		name = "Capture The Flag Win Condition",
		desc = "Tracks how many times a player has captured a flag; if a player has captured enough, it wins.",
		author = "Max Lerner, Roy Wiggins, Nemo (B. Tyler), FLOZi",
		date = "2010-11-09",
		license = "Public domain",
		layer = 0,
		enabled = true
	}
end


-- function localisations
-- Synced Read
local GetUnitDefID						= Spring.GetUnitDefID
local GetUnitRulesParam				=	Spring.GetUnitRulesParam
local GetUnitTeam							= Spring.GetUnitTeam
-- Synced Ctrl
local SetUnitRulesParam				= Spring.SetUnitRulesParam

-- constants
local FLAGS_TO_WIN = 4	--THIS IS WHERE WE SET THE TARGET NUMBER OF FLAG CAPTURES TO WIN!!!!!----
local GAIA_TEAM_ID		= Spring.GetGaiaTeamID()

local oldFlagOwners
local teamFlagConversions = {}
if (gadgetHandler:IsSyncedCode()) then

--SYNCED

local function popTeamTimeTable()
	--Initializes each team's flagsecs of control to 0
	--Dictionary of teamID => time
	for i, teamID in pairs(Spring.GetTeamList()) do
		if teamID ~= GAIA_TEAM_ID then
			teamFlagConversions[teamID] = 0
		end
	end
end
popTeamTimeTable()

local function copy(t)
  local t2 = {}
  for k,v in pairs(t) do
    t2[k] = v
  end
  return t2
end


function gadget:GameFrame(t)
	if GG['donePicking'] == true then
		if t == 6 then
			oldFlagOwners = copy(GG['flagOwners'])
			if GG['flags'][1]~=nil then
				local flagDefID = GetUnitDefID(GG['flags'][1])
				local flagUD = UnitDefs[flagDefID]

			end

		end
		
		if (t % (30) < 0.1) and t > 6 then	-- every second
			if GG['flags'][1]~=nil then
				for i = 1, #GG['flags'] do
					flagID = GG['flags'][i]
					teamID = GetUnitTeam(flagID)
					if teamID ~= GAIA_TEAM_ID and teamID ~= nil then -- Neutral flags do not work towards win cond
						if (oldFlagOwners[flagID]) ~= teamID then
							--Spring.Echo(teamID,"Captured a flag:",oldFlagOwners[flagID],"~=",teamID)
							teamFlagConversions[teamID] = teamFlagConversions[teamID]+1 or 1
						end
					end
				end
				for teamID, score in pairs(teamFlagConversions) do
					if score >= FLAGS_TO_WIN then
					--IF A PLAYER HAS WON:
						if teamID == GG['conversions'][1] or teamID == GG['conversions'][2] then
							Spring.Echo(teamID .. " WINS WITH " .. teamFlagConversions[teamID] .. " FLAGS")
							GG.gameOver(teamID, -1)
							gadgetHandler:RemoveGadget()
						end
					end

				end		
			end
			oldFlagOwners = copy(GG['flagOwners'])
		end
		
		--[[
		if (t % (30*5) < 0.1) and t > 6 then --Every 5 seconds
			for teamID, score in pairs(teamFlagConversions) do
				Spring.Echo("TeamID " .. teamID .. "	FLAGS: " .. score)
			end
		end
		--]]
		if (t % (1*30) < 0.1) then
			if GG['conversions'][1] == -1 and GG['conversions'][2] == -1 then
				--Spring.Echo("removing CTF gadget")
				gadgetHandler:RemoveGadget()
			end
		end
	end	
end

else
--UNSYNCeED
end